package com.kuding.oss.kodo;

import com.kuding.props.qiniuyun.QiniuyunKodoProperty;
import com.qiniu.common.Zone;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.util.Auth;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;


public class QiniuyunKodoComponent {
    private final Log logger = LogFactory.getLog(getClass());

    private QiniuyunKodoProperty qiniuyunKodoProperty;

    private final String jsonContentType = "application/json;charset=utf-8";

    public QiniuyunKodoComponent(QiniuyunKodoProperty qiniuyunKodoProperty) {
        this.qiniuyunKodoProperty = qiniuyunKodoProperty;
    }

    public QiniuyunKodoComponent() {
        super();
    }

    /**
     * 获取client
     * @return
     */
    public Auth getMac() {
        Auth auth = Auth.create(qiniuyunKodoProperty.getAccessKeyId(),qiniuyunKodoProperty.getSecret());
        return auth;
    }

    /**
     * 获取token凭证
     * @return
     */
    public String getUpToken(){
        Auth auth = getMac();
        String upToken = auth.uploadToken(qiniuyunKodoProperty.getDefaultBucket());
        return upToken;
    }

    /**
     * 获取七牛存储文件总数量
     * @return
     */
    public int getFileListSize(){
        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Zone.zone2());
        //...其他参数参考类注释
        BucketManager bucketManager = new BucketManager(getMac(), cfg);
        //每次迭代的长度限制，最大1000，推荐值 1000
        int limit = 1000;
        //指定目录分隔符，列出所有公共前缀（模拟列出目录效果）。缺省值为空字符串
        String delimiter = "";
        //列举空间文件列表
        BucketManager.FileListIterator fileListIterator = bucketManager.createFileListIterator(qiniuyunKodoProperty.getDefaultBucket(), "", limit, delimiter);
        int fileCount = 0;
        while (fileListIterator.hasNext()) {
            //处理获取的file list结果
            fileCount = fileCount + fileListIterator.next().length;
        }
        return fileCount;
    }


}
